﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.CommonClass;

namespace HuaCheng.Views
{
    /// <summary>
    /// 设置工步颜色
    /// </summary>
    public partial class StepColor : Form
    {
        /// <summary>
        /// 初始化页面
        /// </summary>
        public StepColor()
        {           
            InitializeComponent();
            LoadPanelColor();
        }
        /// <summary>
        /// Panel加载已设置的颜色
        /// </summary>
        public void LoadPanelColor()
        {
            foreach (Control pnlControl in this.pnlContain.Controls)
            {
                if (pnlControl is Panel)
                {
                    Color color = IniControl.CalColor(pnlControl.Name);
                    pnlControl.BackColor = color;  
                }
            }     
        }
        /// <summary>
        /// 读取ini获得表格每一行的颜色
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, Color> GetStepColor()
        {
            Dictionary<string, Color> nodeColor = new Dictionary<string, Color>();
            foreach (Control pnlControl in this.pnlContain.Controls)
            {
                if (pnlControl is Panel)
                {
                    Color color = IniControl.CalColor(pnlControl.Name);
                    string buttonName = "btn" + pnlControl.Name.Substring(3);
                    string buttonText = string.Empty;
                    Button btnControl = (Button)this.pnlContain.Controls[buttonName];
                    buttonText = btnControl.Text;
                    nodeColor.Add(buttonText, color);
                }
            }
            return nodeColor;
        }
        
        /// <summary>
        /// 点击按钮选择颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnColor_Click(object sender, EventArgs e)
        {        
            Button btn = sender as Button;
            string btnName=btn.Name;
            string pnlName = "pnl" + btnName.Substring(3);
            Panel pnlControl = (Panel)this.pnlContain.Controls[pnlName];
            colordlgStep.Color = pnlControl.BackColor;
            colordlgStep.ShowDialog();
            pnlControl.BackColor = this.colordlgStep.Color;
         }
        /// <summary>
        /// 将选择的颜色保存为ini
        /// </summary>
        private void SaveColor()
        {
            foreach (Control control in this.pnlContain.Controls)
            {
                if (control is Panel) 
                {
                    string color = ((control.BackColor.ToArgb())).ToString("x8");
                    int red = Convert.ToInt32(color.Substring(2, 2), 16);
                    int green = Convert.ToInt32(color.Substring(4, 2), 16);
                    int blue = Convert.ToInt32(color.Substring(6, 2), 16);
                    int colorValue = (65536 * blue) + (256 * green) + red;
                    string strSec = "step color";
                    string path = DicHelper.RetrieveConfigPath();
                    ReadAndWriteIni.WritePrivateProfileString(strSec, control.Name, colorValue.ToString(),path);
                }
            }          
        }
        /// <summary>
        /// 确认按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveColor();
            this.Close();
        }
        /// <summary>
        /// 退出按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 重置按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.pnlContain.Controls)
            {
                if (control is Panel)
                {
                    control.BackColor = Color.White;
                }
                
            }
        }

        private void StepColor_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }

        #region 国际化
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("SettingStepColor");
            this.btnSave.Text = ResourceCulture.GetString("Save2");
            this.btnQuit.Text = ResourceCulture.GetString("Quit");
            this.btnReset.Text = ResourceCulture.GetString("Reset");
        }
        #endregion
    }
}
